import { app, BrowserWindow, ipcMain } from "electron"
import { join } from "path"
import * as eve from "./work"
function createWindow() {
  const win = new BrowserWindow({
    show: false,
    webPreferences: {
      nodeIntegration: false, // is default value after Electron v5
      contextIsolation: true, // protect against prototype pollution
      preload: join(__dirname, "preload.js"),
    },
  })
  win.maximize()
  if (process.env.NODE_ENV == "dev") {
    win.loadURL("http://localhost:3000")
    win.webContents.openDevTools()
  } else {
    win.setMenu(null)
    win.loadFile("dist/index.html")
  }
  win.show()
}
app.whenReady().then(() => {
  regEvents()
  createWindow()
  app.on("activate", function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})
function regEvents() {
  ipcMain.handle('addProject', eve.selectFile)
  ipcMain.handle('getProject', eve.getProject)
  ipcMain.handle('getFile', eve.getFile)
  ipcMain.handle("saveFile", eve.saveFile)
  ipcMain.handle("readDb", eve.readDb)
  ipcMain.on("watch", eve.watchDir)
}